import key from 'keymaster';

function delay(timeout){
    return new Promise(resolve => {
      setTimeout(resolve, timeout);
    });
  }
export default {
    namespace: 'count',
    state:{
        record:0,
        current:0
    },
    reducers:{
        add(state){
            const newCurrent = state.current +1;
            return {
                ...state,
                record:newCurrent>state.record?newCurrent:state.current,
                current:newCurrent
            }
        },
        minus(state){
            return {
                ...state,
                current:state.current -1
            }
        }
    },
    effects:{
        *add(action,{call,put}){
            yield call(delay,2000);
            yield put({ type:'minus'});
        },
    },
    subscriptions: {
        keyboardWatcher({ dispatch }) {
          key('⌘+up, ctrl+up', () => { dispatch({type:'add'}) });
        },
      },
// 然后 state 里的 record 表示 highest record，current 表示当前速度。
}